/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.improvement.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.improvement.bean.SecurityReportBean;
import com.koron.improvement.service.api.SecurityReportService;
import com.koron.improvement.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "securityReport", description = "安全报告")
@RestController
@RequestMapping(value = "/securityReport")
public class SecurityReportController {

    @Autowired
    private SecurityReportService securityReportService;

    @ApiOperation("列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentCode", value = "部门code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srName", value = "名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srNo", value = "编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fillDateStart", value = "填报日期-开始(yyyy-MM-dd)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fillDateStop", value = "填报日期-结束(yyyy-MM-dd)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "reportTypeCode", value = "报告类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "reportLevel", value = "报告等级", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryForList.htm", method = RequestMethod.GET)
    public MessageBean queryForList(@ApiIgnore String corpCode, @ApiIgnore String departmentCode,
                                    @ApiIgnore String srName, @ApiIgnore String srNo, @ApiIgnore String fillDateStart,
                                    @ApiIgnore String fillDateStop, @ApiIgnore String reportTypeCode, @ApiIgnore Integer reportLevel,
                                    @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        return securityReportService.queryForList(corpCode, departmentCode,
                srName, srNo, fillDateStart,
                fillDateStop, reportTypeCode, reportLevel,
                page, pageSize);
    }

    @ApiOperation("根据ID查询安全报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/queryById.htm", method = RequestMethod.GET)
    public MessageBean queryById(@ApiIgnore String id) {
        return securityReportService.queryById(id);
    }

    @ApiOperation("根据ID删除安全报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteById.htm", method = RequestMethod.POST)
    @Log(title = "根据ID删除安全报告", businessModule = "安全报告", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean deleteById(@ApiIgnore String id) {
        return securityReportService.deleteById(id);
    }

    @ApiOperation("新增安全报告(保存)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "水司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentCode", value = "部门code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentName", value = "部门名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srName", value = "名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srNo", value = "编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "fillDate", value = "填报日期(yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportTypeCode", value = "报告类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportType", value = "报告类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportLevel", value = "报告等级", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "generalize", value = "概括", paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/save.htm", method = RequestMethod.POST)
    @Log(title = "新增安全报告(保存)", businessModule = "安全报告", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SecurityReportBean bean,
                            @RequestParam(value = "files") MultipartFile[] files) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        // 新增，状态为 0 草稿
        String status = "0";
        return securityReportService.insert(user, bean, files, status,
                null, null, null,
                null, null, null);
    }

    @ApiOperation("新增安全报告(提交)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "水司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentCode", value = "部门code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentName", value = "部门名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srName", value = "名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srNo", value = "编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "fillDate", value = "填报日期(yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportTypeCode", value = "报告类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportType", value = "报告类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportLevel", value = "报告等级", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "generalize", value = "概括", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subWaterDeparCodes", value = "下属水司会签codes", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subCompChargeBusinLeaderCode", value = "下属公司业务分管领导code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaSecurityManagDeparCode", value = "片区安全管理部code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaChargeSafetyLeaderCode", value = "片区分管安全领导code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "generalManagerCode", value = "片区总经理code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaChargeLeaderCode", value = "片区分管领导code", paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/submit.htm", method = RequestMethod.POST)
    @Log(title = "新增安全报告(提交)", businessModule = "安全报告", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean submit(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SecurityReportBean bean,
                              @RequestParam(value = "files") MultipartFile[] files,
                              @ApiIgnore String subWaterDeparCodes, @ApiIgnore String subCompChargeBusinLeaderCode,
                              @ApiIgnore String areaSecurityManagDeparCode, @ApiIgnore String areaChargeSafetyLeaderCode,
                              @ApiIgnore String generalManagerCode, @ApiIgnore String areaChargeLeaderCode) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        // 新增，状态为 1 审批中
        String status = "1";
        return securityReportService.insert(user, bean, files, status, subWaterDeparCodes, subCompChargeBusinLeaderCode,
                areaSecurityManagDeparCode, areaChargeSafetyLeaderCode, generalManagerCode, areaChargeLeaderCode);
    }

    @ApiOperation("修改安全报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "水司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentCode", value = "部门code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentName", value = "部门名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srName", value = "名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "srNo", value = "编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "fillDate", value = "填报日期(yyyy-MM-dd HH:mm:ss)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportTypeCode", value = "报告类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportType", value = "报告类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reportLevel", value = "报告等级", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "generalize", value = "概括", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "accessorys", value = "附件(参数格式为JSON字符串，" +
                    "如( [{'filename':'xxx','fileUrl':'xxx'},{'filename':'xxx','fileUrl':'xxx'}] ))", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态：0草稿、1审核中、2审核通过", dataType = "String", paramType = "query"),

            @ApiImplicitParam(name = "subWaterDeparCodes", value = "下属水司会签codes", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subCompChargeBusinLeaderCode", value = "下属公司业务分管领导code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaSecurityManagDeparCode", value = "片区安全管理部code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaChargeSafetyLeaderCode", value = "片区分管安全领导code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "generalManagerCode", value = "片区总经理code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaChargeLeaderCode", value = "片区分管领导code", paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/update.htm", method = RequestMethod.POST)
    @Log(title = "修改安全报告", businessModule = "安全报告", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore SecurityReportBean bean,
                              @RequestParam(value = "files") MultipartFile[] files,
                              @ApiIgnore String subWaterDeparCodes, @ApiIgnore String subCompChargeBusinLeaderCode,
                              @ApiIgnore String areaSecurityManagDeparCode, @ApiIgnore String areaChargeSafetyLeaderCode,
                              @ApiIgnore String generalManagerCode, @ApiIgnore String areaChargeLeaderCode) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        // status
        String status = bean.getStatus();
        return securityReportService.update(user, bean, files, status, subWaterDeparCodes, subCompChargeBusinLeaderCode,
                areaSecurityManagDeparCode, areaChargeSafetyLeaderCode, generalManagerCode, areaChargeLeaderCode);
    }
}
